package net.chasing.androidbaseconfig.util.bugfixed;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;

import java.lang.reflect.Field;

/**
 * 解决内存泄露
 */
public class FixMemLeak {
    private static Field field;
    private static boolean hasField = true;

    //解决华为InputMethodManager.mLastSrvView造成的内存泄露
    public static void fixLeak(Context context) {
        if (!hasField || context == null) {
            return;
        }
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }

        String[] arr = new String[]{"mLastSrvView"};
        for (String param : arr) {
            try {
                if (field == null) {
                    field = imm.getClass().getDeclaredField(param);
                }
                field.setAccessible(true);
                field.set(imm, null);
            } catch (Throwable ignored) {
                hasField = false;
            }
        }
    }
}
